//
//  UIView+wj_View.swift
//  Lieutenant515_swfit
//
//  Created by wsj on 16/11/4.
//  Copyright © 2016年 wsj. All rights reserved.
//

import UIKit
public typealias QMBtnAction = (UIButton)->();
// 改进写法【推荐】
struct RuntimeKey {
    static let wj_btnActionKey = UnsafeRawPointer.init(bitPattern: "jsdfgusdfg".hashValue)
    
}
extension UIView {
   
    public var x:CGFloat {
        get{
            return self.frame.origin.x;
        }
        set{
            var r = self.frame;
            r.origin.x = newValue;
            self.frame = r;
        }
    }
    public var y:CGFloat {
        get{
            return self.frame.origin.y;
        }
        set{
            var r = self.frame;
            r.origin.y = newValue;
            self.frame = r;
            
        }
    }
    public var height:CGFloat{
        get{
            return self.frame.height;
        }
        set{
            var r = self.frame;
            r.size.height = newValue;
            self.frame = r;
            
        }
    }
    public var width:CGFloat {
        get{
            return self.frame.size.width;
        }
        set{
            var r = self.frame;
            r.size.width  = newValue;
            self.frame = r;
            
        }
    }
    public var centerX:CGFloat {
        get{
            return self.center.x;
        }
        set{
            var r = self.center;
            r.x = newValue;
            self.center = r;
        }
    }
    public var centerY:CGFloat{
        get{
            return self.center.y;
        }
        set{
            var r = self.center;
            r.y = newValue;
            self.center = r;
        }
            
    }
    public var size:CGSize{
        get{
            return self.frame.size;
        }
        set{
            var frame = self.frame;
            frame.size = newValue;
            self.frame = frame;
        }
        
    }
    /// 获取当前控件所在的VC
    ///
    /// - parameter view: 当前的控件
    ///
    /// - returns: 返回控制器
    public func wj_getViewController()->UIViewController{
        var VC:UIViewController?;
        var next  = self.next;
        while (next != nil) {
            if (next?.isKind(of: UIViewController.self))!{
                VC = next as! UIViewController?;
                break;
            }
            next = next?.next;
        }
      return VC!;
    }
    
    /// 注意次方法会创建一个新的view返回出去，调用的时候请不要对多次调用
    ///
    /// - Returns:
    static  func wj_mask()-> QMViewModel {
        let view = self.init();
        let model = QMViewModel.init();
        model.wj_view = view;
        return model;
    }
    
    
    
}
extension UILabel {
   public  func wj_text(text:String) -> UIView{
        return self;
    }
}
extension UIButton {
    
    var qm_btnAction: QMBtnAction? {
        set {
            objc_setAssociatedObject(self, RuntimeKey.wj_btnActionKey!, newValue, .OBJC_ASSOCIATION_COPY_NONATOMIC)
        }
        get {
            return  objc_getAssociatedObject(self, RuntimeKey.wj_btnActionKey!) as? QMBtnAction
        }
    }
    /// 按钮的点击事件
    ///
    /// - Parameter button:
    @objc  func wj_buttonAction(button:UIButton){
        if let action = self.qm_btnAction {
            action(button);
        }
    }
}

